import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class SampleRecommender {

	public static void main(String[] args) throws IOException, Exception {
		// 加载包含用户偏好评分的数据
		DataModel model = new FileDataModel(new File("data.csv"));
		// 使用皮尔逊相关系数
		UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
		// 指定距离最近的一定百分比的用户作为邻居
		UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
		// 构建基于用户的推荐器
		UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
		// 为2号用户推荐3个物品
		List<RecommendedItem> recommendations = recommender.recommend(2, 3);
		
		for (RecommendedItem recommendation : recommendations) {
			System.out.println(recommendation);
		}
	}
}
